﻿using Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Xml.Linq;

namespace Run
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        AutoUpdateConfigModel updateXML = new AutoUpdateConfigModel();
        LocalXMLModel localXML = new LocalXMLModel();

        public MainWindow()
        {
            InitializeComponent();
            try
            {
                localXML= GetLocalInfoContent();
                if (localXML == null)
                {
                    TipMessageAndCloseWin("丢失配置文件，请联系工程师！");

                    return;
                }
                if(!PingServer())
                {
                    TipMessageAndCloseWin("连接服务器失败，请检查网络连接；您还可以联系管理员或者稍后再试！");

                    return;
                }
                if (!CheckRunProcess())
                {
                    TipMessageAndCloseWin("手麻系统已启动，请勿重复点击！");

                    return;
                }
                LogHelper.Fatal(this.GetType(), "start GetUpdateXMLContent");
                GetUpdateContent();
                LogHelper.Fatal(this.GetType(), "end GetUpdateXMLContent");
                LogHelper.Fatal(this.GetType(), "start KillAllProcesses");
                KillAllProcesses();
                LogHelper.Fatal(this.GetType(), "end KillAllProcesses");
                LogHelper.Fatal(this.GetType(), "start DownloadStartExe");
                DownloadUpdateFiles();
                LogHelper.Fatal(this.GetType(), "end DownloadStartExe");
                LogHelper.Fatal(this.GetType(), "start ProcessStartExe");
                Process.Start(updateXML.StartExeLocalPath);
                LogHelper.Fatal(this.GetType(), "end ProcessStartExe");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                LogHelper.Fatal(this.GetType(), ex);
            }
            finally
            {
                CloseSys();
            }
        }

        private void TipMessageAndCloseWin(string msg)
        {
            MessageBox.Show(msg, "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
            CloseSys();
        }

        private bool PingServer()
        {
            return IPHelper.PingIP(localXML.SerIp);
        }

        private void CloseSys()
        {
            Close();
        }


        private bool CheckRunProcess()
        {
            Process current = Process.GetCurrentProcess();
            Process[] runPros = Process.GetProcessesByName("Run");
            if (runPros != null && runPros.Length!=0)
            {
                foreach (Process pro in runPros)
                {
                    if (pro.Id != current.Id)
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        private LocalXMLModel GetLocalInfoContent()
        {
            try
            {
                return (new ClientAndServerHelper()).GetLocalXmlContent();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void GetUpdateContent()
        {
            try
            {
                updateXML = ClientAndServerHelper.Instance.UpdateXMLContent;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        private void InitMongo()
        {
            try
            {

            }
            catch (Exception ex)
            {
                LogHelper.Error(this.GetType(), ex);
                throw new XMLParsingFailedException();
            }
        }

        private void DownloadUpdateFiles()
        {
            try
            {
                ClientAndServerHelper.Instance.DownloadFiles(false, true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 杀掉所有进程
        /// </summary>
        private void KillAllProcesses()
        {
            try
            {
                Process cur = Process.GetCurrentProcess();
                LogHelper.Fatal(this.GetType(), "updateXML.KillProcess:" + updateXML.KillProcess.Count);
                foreach (string proName in updateXML.KillProcess)
                {
                    LogHelper.Fatal(this.GetType(), "Process:" + proName);
                    ProcessHelper.KillProcessByName(proName, cur);
                    LogHelper.Fatal(this.GetType(), "killProcess:" + proName);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(this.GetType(), ex);
                throw new KillProcessFailedException();
            }
        }
    }
}
